package com.bat.conf;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义springboot Actuator metrics
 * https://blog.csdn.net/myherux/article/details/81781199
 * @Description:
 * @Author: zpf
 * @Date: 2020/7/21 下午7:34
 */
@Component
@Slf4j
public class ThreadPoolMetrics implements MeterBinder {

    public Counter job1Counter;

    public Counter job2Counter;

    public Gauge gauge;

    public Map<String, Double> map;

    ThreadPoolMetrics() {
        map = new HashMap<>();
    }

    @Override
    public void bindTo(final MeterRegistry meterRegistry) {
        this.job1Counter =
            Counter.builder("my_job").tags(new String[] {"name", "job1"}).description("Job 1 execute count")
                .register(meterRegistry);

        this.job2Counter =
            Counter.builder("my_job").tags(new String[] {"name", "job2"}).description("Job 2 execute count")
                .register(meterRegistry);
        Gauge.builder("threadActiveCount", map, x -> x.get("threadActiveCount")).tags("name", "threadActiveCount")
            .description("").register(meterRegistry);
        Gauge.builder("threadActiveCount", map, x -> x.get("theadPoolSize")).tags("name", "theadPoolSize")
            .description("").register(meterRegistry);
        Gauge.builder("threadKeepActiveCount", map, x -> x.get("threadKeepActiveCount"))
            .tags("name", "threadKeepActiveCount").description("").register(meterRegistry);

    }
//    @Autowired
//    private EncryptFeign encryptFeign;
//    @Autowired
//    private ThreadPoolMetrics threadPoolMetrics;
//    @Autowired
//    private ThreadPoolTaskExecutor executor;
//

//    /**      http://localhost:8080/actuator/prometheus  http://localhost:8080/actuator/metrics/my_job_gauge
//     * 根据代号查明文
//     *
//     * @param form 业务类型（需要先找架构新增）
//     * @return 明文
//     */
//    @ApiOperation("手机代号查询手机号码")
//    @PostMapping(value = "/query_by_code")
//    Response<String> queryByCode(@RequestBody @Validated final PhoneQueryByCodeForm form) {
//        threadPoolMetrics.map.put("threadActiveCount", (double)executor.getActiveCount());
//        threadPoolMetrics.map.put("theadPoolSize", (double)executor.getPoolSize());
//        threadPoolMetrics.map.put("threadKeepActiveCount", (double)executor.getKeepAliveSeconds());
//        threadPoolMetrics.job1Counter.increment();
//        threadPoolMetrics.job2Counter.increment();
//        return success(encryptFeign
//            .getData(form.getBizType(), form.getUrl(), "", this.getCurrentEmployee().getId().toString(), form.getCode())
//            .getData());
//    }
}
